package com.example.restatr.Atcivity.Movie.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Movie.MovieInfoAt;
import com.example.restatr.Atcivity.Movie.Bean.MovieFilmBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class MovieHotFragment extends Fragment {
    private Banner banner;
    private LinearLayout group;
    private RecyclerView list;
    public static List<MovieFilmBean.RowsBean> rowsBeanList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie_hot, container, false);
        banner = view.findViewById(R.id.banner);
        group = view.findViewById(R.id.group);
        list = view.findViewById(R.id.list);
        Tools.SetBanner("/prod-api/api/movie/rotation/list", "", "rows",
                "advImg",banner, getActivity());
        GetMovieFilm();
        return view;
    }

    private void GetMovieFilm() {
        rowsBeanList.clear();
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/movie/film/list", MainActivity.sp.getString("token", ""));
            rowsBeanList.addAll(new Gson().fromJson(token, MovieFilmBean.class).getRows());
            if (getActivity() != null) {
                getActivity().runOnUiThread(() -> {
                    list.setAdapter(new adapter());
                });
            }
        }).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.adapterViewHolder> {

        @NonNull
        @Override
        public adapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.movie_hot_item_layout, parent, false);
            return new adapterViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull adapterViewHolder holder, int position) {

            Glide.with(getActivity())
                    .load(Tools.ip + rowsBeanList.get(position).getCover())
                    .transform(new RoundedCorners(15))
                    .error(R.drawable.bj)
                    .into(holder.cover);
            holder.name.setText(rowsBeanList.get(position).getName());
            holder.likeAndFavorite.setText(rowsBeanList.get(position).getLikeNum() + "想看" + " " + rowsBeanList.get(position).getFavoriteNum() + "看过");
            holder.playDate.setText("上映时间：" + rowsBeanList.get(position).getPlayDate());
            holder.rating.setRating(rowsBeanList.get(position).getScore());
            holder.score.setText(String.valueOf(rowsBeanList.get(position).getScore()));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MovieInfoAt.class);
                    intent.putExtra("电影信息", new Gson().toJson(rowsBeanList.get(position)));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeanList.size();
        }

        class adapterViewHolder extends RecyclerView.ViewHolder {
            private ImageView cover;
            private TextView name;
            private TextView likeAndFavorite;
            private TextView playDate;
            private RatingBar rating;
            private TextView score;
            private CardView btn;

            public adapterViewHolder(@NonNull View itemView) {
                super(itemView);
                cover = itemView.findViewById(R.id.cover);
                name = itemView.findViewById(R.id.name);
                likeAndFavorite = itemView.findViewById(R.id.likeAndFavorite);
                playDate = itemView.findViewById(R.id.playDate);
                rating = itemView.findViewById(R.id.rating);
                score = itemView.findViewById(R.id.score);
                btn = itemView.findViewById(R.id.btn);
            }
        }
    }


}